package com.algrithom.find;

import com.algrithom.array.Solution10;

/**
 * 33. 搜索旋转排序数组
 */
public class Solution2 {
    public static void main(String[] args){
        int[] nums = new int[]{4,5,6,7,0,1,2};
        Solution2 solution2 = new Solution2();
        System.out.println(solution2.search(nums,0));
    }
    public int search(int[] nums, int target) {
        int n = nums.length;
        int l = 0, r = n - 1;
        while (l <= r) {
            int mid = (l + r) / 2;
            if (nums[mid] == target) {
                return mid;
            }
            if (nums[0] <= nums[mid]) {
                if (nums[0] <= target && target < nums[mid]) {
                    r = mid - 1;
                } else {
                    l = mid + 1;
                }
            } else {
                if (nums[mid] < target && target <= nums[n - 1]) {
                    l = mid + 1;
                } else {
                    r = mid - 1;
                }
            }
        }
        return -1;
    }
}
